package com.houtai.service.activity.works;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdActivityWorks;
import com.houtai.bean.dto.activity.works.ActivityWorksListDto;
import com.houtai.bean.dto.activity.works.TdActivityWorksListDto;
import com.houtai.bean.vo.activity.works.TdActivityWorksDetail;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TdActivityWorksDao;
import com.houtai.dao.TsJzAdminOperationDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/4/22.
 */
@Service
public class ActivityWorksService {
    private static Logger logger = Logger.getLogger(ActivityWorksService.class);
    @Autowired
    private TdActivityWorksDao tdActivityWorksDao;


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    /**
     * @Purpose:    活动作品列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public RetInfo activityWorksList(HttpServletRequest request, ActivityWorksListDto dto) {
        String logInfo = this.getClass().getName() + ":activityWorksList:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), "works_id DESC");
            List<TdActivityWorks> lists = tdActivityWorksDao.findActivityWorksList(dto);
            PageInfo<TdActivityWorks> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());

            map.put("activity_id", dto.getActivity_id());
            map.put("works_name", dto.getWorks_name());
            map.put("works_type", dto.getWorks_type());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查看活动作品列表" , request);
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    /**
     * @Purpose:    活动作品列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public RetInfo commentsList(HttpServletRequest request, TdActivityWorksListDto dto) {
        String logInfo = this.getClass().getName() + ":commentsList:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), "works_id DESC");
            List<TdActivityWorks> lists = tdActivityWorksDao.findTdActivityWorksList(dto);
            PageInfo<TdActivityWorks> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());

            map.put("state", dto.getState());
            map.put("works_name", dto.getWorks_name());
            map.put("works_type", dto.getWorks_type());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查看活动作品列表" , request);
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   更新作品状态
     * @param     works_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateActivityWorksState(int works_id, int state, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateActivityWorksState:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdActivityWorksDao.updateTdActivityWorksState(works_id, state);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改作品状态,id:" + works_id
                    + "状态：" + state,request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public TdActivityWorksDetail findTdCourseDetail(Integer works_id) {
        return tdActivityWorksDao.findTdActivityWorksDetail(works_id);
    }
}
